package com.axend.aerosense.room.ui.fragment;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.DialogFragment;

import com.jzxiang.pickerview.adapters.NumericWheelAdapter;
import com.jzxiang.pickerview.config.NumberConfig;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.data.source.NumRepository;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.jzxiang.pickerview.utils.PickerContants;
import com.jzxiang.pickerview.utils.Utils;
import com.jzxiang.pickerview.wheel.OnWheelChangedListener;
import com.jzxiang.pickerview.wheel.WheelView;
import com.axend.aerosense.common.R;
import com.axend.aerosense.common.databinding.CommonFragmentNumpickerLayoutBinding;
public class BottomNumberPickerDialog extends DialogFragment implements View.OnClickListener {

    private WheelView wheelView;
    private NumericWheelAdapter mNumberAdapter;
    private NumberConfig mNumberConfig;
    private NumRepository mRepository;;
    private CommonFragmentNumpickerLayoutBinding dialogBinding;
    private OnWheelChangedListener numberListener = new OnWheelChangedListener() {
        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
//            updateMonths();
        }
    };


    private static BottomNumberPickerDialog newIntance(NumberConfig pickerConfig) {
        BottomNumberPickerDialog timePickerDialog = new BottomNumberPickerDialog();
        timePickerDialog.initialize(pickerConfig);
        return timePickerDialog;
    }

    private void initialize(NumberConfig pickerConfig) {
        mNumberConfig = pickerConfig;
        mRepository = new NumRepository(mNumberConfig);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Activity activity = getActivity();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //加这句话去掉自带的标题栏
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.common_fragment_numpicker_layout, container, false);
        return dialogBinding.getRoot();
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
        //设置背景透明
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @Override
    public void onResume() {
        super.onResume();
        int height = getResources().getDimensionPixelSize(com.jzxiang.pickerview.R.dimen.picker_height);

        Window window = getDialog().getWindow();
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, height);//Here!
        window.setGravity(Gravity.BOTTOM);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }


    void initView() {
        wheelView = dialogBinding.commonNumber;
        dialogBinding.commonSure.setOnClickListener(v->sureClicked());
        dialogBinding.commonCancel.setOnClickListener(v->dismiss());
        Utils.showViews(wheelView);
        wheelView.addChangingListener(numberListener);
        initData();
    }

    void initData() {
        int minValue = mRepository.getMinValue();
        int maxValue = mRepository.getMaxValue();

        mNumberAdapter = new NumericWheelAdapter(getContext(), minValue, maxValue, PickerContants.FORMAT_0, mNumberConfig.mUnit, mNumberConfig.step);
        mNumberAdapter.setConfig(mNumberConfig);
        wheelView.setViewAdapter(mNumberAdapter);
        wheelView.setCurrentItem(mRepository.getDefaultValue() - minValue);
    }

    public int getCurrentValue() {
        return wheelView.getCurrentItem() * mRepository.getStep() + mRepository.getMinValue();
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == com.jzxiang.pickerview.R.id.tv_cancel) {
            dismiss();
        } else if (i == com.jzxiang.pickerview.R.id.tv_sure) {
            sureClicked();
        }
    }

    /*
    * @desc This method is called when onClick method is invoked by sure button. A Calendar instance is created and 
    *       initialized. 
    * @param none
    * @return none
    */
    void sureClicked() {
        if (mNumberConfig.mCallBack != null) {
            mNumberConfig.mCallBack.onDateSet(this, getCurrentValue());
        }
        dismiss();
    }

    public static class Builder {
        NumberConfig mPickerConfig;

        public Builder() {
            mPickerConfig = new NumberConfig();
        }

        public Builder setType(Type type) {
            mPickerConfig.mType = type;
            return this;
        }

        public Builder setThemeColor(int color) {
            mPickerConfig.mThemeColor = color;
            return this;
        }

        public Builder setCancelStringId(String left) {
            mPickerConfig.mCancelString = left;
            return this;
        }

        public Builder setSureStringId(String right) {
            mPickerConfig.mSureString = right;
            return this;
        }

        public Builder setTitleStringId(String title) {
            mPickerConfig.mTitleString = title;
            return this;
        }

        public Builder setToolBarTextColor(int color) {
            mPickerConfig.mToolBarTVColor = color;
            return this;
        }

        public Builder setWheelItemTextNormalColor(int color) {
            mPickerConfig.mWheelTVNormalColor = color;
            return this;
        }

        public Builder setWheelItemTextSelectorColor(int color) {
            mPickerConfig.mWheelTVSelectorColor = color;
            return this;
        }

        public Builder setWheelItemTextSize(int size) {
            mPickerConfig.mWheelTVSize = size;
            return this;
        }

        public Builder setCyclic(boolean cyclic) {
            mPickerConfig.cyclic = cyclic;
            return this;
        }

        public Builder setMinValue(int value) {
            mPickerConfig.mMinValue = value;
            return this;
        }

        public Builder setMaxValue(int value) {
            mPickerConfig.mMaxValue = value;
            return this;
        }

        public Builder setDefValue(int value) {
            mPickerConfig.mDefValue = value;
            return this;
        }

        public Builder setUnit(String unit) {
            mPickerConfig.mUnit = unit;
            return this;
        }

        public Builder setCallBack(OnDateSetListener listener) {
            mPickerConfig.mCallBack = listener;
            return this;
        }

        public Builder setStep(int step){
            mPickerConfig.step = step;
            return this;
        }

        public BottomNumberPickerDialog build() {
            return newIntance(mPickerConfig);
        }

    }
}
